
package com.wobaby.util;

import java.io.PrintStream;
import java.io.PrintWriter;

public interface RenditionException
{
    public static class Support
    {

        public static String getMessage(Exception exception)
        {
            StringBuffer message = new StringBuffer();
            RenditionException renditionException = null;
            if(exception instanceof RenditionException)
                renditionException = (RenditionException)exception;
            String exceptionMessage = "";
            if(renditionException != null)
                exceptionMessage = renditionException.getThisMessage();
            else
                exceptionMessage = exception.getMessage();
            if(exceptionMessage != null)
                message.append(exceptionMessage);
            if(renditionException != null && renditionException.hasException())
            {
                Throwable wrappedException = renditionException.getException();
                if(message != null && message.length() > 0)
                    message.append(" ");
                message.append("{ wrapped ");
                message.append(wrappedException.getClass().getName());
                message.append(" ");
                String wrappedMessage = wrappedException.getMessage();
                if(wrappedMessage != null && wrappedMessage.length() > 0)
                {
                    message.append(": \"");
                    message.append(wrappedMessage);
                    message.append("\" ");
                }
                message.append("}");
            }
            return message.toString();
        }

        public static String getMessageShallow(Throwable exception)
        {
            String message = "";
            if(exception instanceof RenditionException)
            {
                RenditionException renditionException = (RenditionException)exception;
                message = renditionException.getThisMessage();
                if((message == null || message.length() == 0) && renditionException.hasException())
                    message = getMessageShallow(renditionException.getException());
            } else
            {
                message = exception.getMessage();
            }
            return message;
        }

        public static String getMessageDeep(Throwable exception)
        {
            if(exception instanceof RenditionException)
            {
                RenditionException renditionException = (RenditionException)exception;
                if(renditionException.hasException())
                    return getMessageDeep(renditionException.getException());
                else
                    return renditionException.getThisMessage();
            } else
            {
                return exception.getMessage();
            }
        }

        public static void printStackTrace(Exception exception)
        {
            printStackTrace(exception, System.err);
        }

        public static void printStackTrace(Exception exception, PrintStream printStream)
        {
            printStackTrace(((Throwable) (exception)), new PrintWriter(printStream));
        }

        public static void printStackTrace(Throwable exception, PrintWriter printWriter)
        {
            if(exception instanceof RenditionException)
            {
                RenditionException renditionException = (RenditionException)exception;
                if(renditionException.hasException())
                {
                    printWriter.println(renditionException.getThisMessage());
                    printStackTrace(renditionException.getException(), printWriter);
                } else
                {
                    renditionException.printThisStackTrace(printWriter);
                }
            } else
            {
                exception.printStackTrace(printWriter);
            }
            printWriter.flush();
        }

        public static void printAllStackTraces(Exception exception)
        {
            printAllStackTraces(exception, System.err);
        }

        public static void printAllStackTraces(Exception exception, PrintStream printStream)
        {
            printAllStackTraces(((Throwable) (exception)), new PrintWriter(printStream));
        }

        public static void printAllStackTraces(Throwable exception, PrintWriter printWriter)
        {
            if(exception instanceof RenditionException)
            {
                RenditionException renditionException = (RenditionException)exception;
                renditionException.printThisStackTrace(printWriter);
                if(renditionException.hasException())
                {
                    printWriter.println("{ wrapped exception");
                    printAllStackTraces(renditionException.getException(), printWriter);
                    printWriter.println("}");
                }
            } else
            {
                exception.printStackTrace(printWriter);
            }
            printWriter.flush();
        }

        private Support()
        {
        }
    }


    public abstract String getMessage();

    public abstract String getMessageShallow();

    public abstract String getMessageDeep();

    public abstract void printStackTrace();

    public abstract void printStackTrace(PrintStream printstream);

    public abstract void printStackTrace(PrintWriter printwriter);

    public abstract void printAllStackTraces();

    public abstract void printAllStackTraces(PrintStream printstream);

    public abstract void printAllStackTraces(PrintWriter printwriter);

    public abstract void setException(Throwable throwable);

    public abstract boolean hasException();

    public abstract Throwable getException();

    public abstract String getThisMessage();

    public abstract void printThisStackTrace(PrintWriter printwriter);
}